package main

import (
	"fmt"
	"time"

	"gitee.com/cjianwen/go/socks"
)

type iTest interface {
	test()
	doTest()
}

type struct1 struct {
}

func (s *struct1) test() {
	fmt.Println("struct1.test")
}

func (s *struct1) doTest() {
	s.test()
}

type struct2 struct {
	*struct1
}

func (s *struct2) test() {
	fmt.Println("struct2.test")
}

func test_code() {
	s := &struct2{}
	s.doTest()

	var i iTest = s

	i.doTest()
	i.test()

	d := []byte{1, 2, 3, 4, 5, 6, 7}
	fmt.Println(d)
	copy(d, d[3:])
	fmt.Println(d)

	c := make(chan int, 1)
	c <- 1
	// c <- 2
	// c <- 3

	// _, o := <-c
	// fmt.Println("o = ", o)

	// for v := range c {
	// 	fmt.Println("v = ", v)
	// }

	// close(c)

	select {
	case c <- 666:
		fmt.Println("666")
	default:
		fmt.Println("default")
	}

	v, ok := <-c
	fmt.Println("v = ", v, ok)
}

// type IMsgHeader interface {
// 	GetHeaderSize() int
// 	ReadHeader(br *BufferUtils) (isOk bool, bodyLen int)
// 	WriteHeader(bw *BufferUtils, bodyLen int) bool
// }

type echoHeader struct{}

func (e *echoHeader) GetHeaderSize() int {
	return 0
}

func (e *echoHeader) ReadHeader(br *socks.BufferUtils) (isOk bool, bodyLen int) {
	return true, 0
}

func (e *echoHeader) WriteHeader(bw *socks.BufferUtils, bodyLen int) bool {
	return true
}

type msgHandler struct{}

func (h *msgHandler) OnConnect(sock socks.SocketConnection) {
	fmt.Println("connect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnDisconnect(sock socks.SocketConnection) {
	fmt.Println("disconnect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnMessage(sock socks.SocketConnection, msg *socks.PackData) {
	fmt.Println("on message ", string(msg.Body))
}

func main() {
	test_code()

	svr := socks.NewSocketServer(socks.SockOptions{HeadHandler: socks.NewDefaultHeadHandler(nil), MsgHandler: &msgHandler{}})
	svr.Open(8008)

	fmt.Println("server opended: 8008")
	for {
		time.Sleep(time.Second)
	}
}
